{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "### 集合运算"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "set() <class 'set'>\n",
      "{1, 'b', 2} <class 'set'>\n",
      "{1, 2, 3, 4, 5} <class 'set'>\n"
     ]
    }
   ],
   "source": [
    "# 集合初始化\n",
    "s = set()\n",
    "\n",
    "print(s, type(s))\n",
    "\n",
    "s.add(1)\n",
    "s.add('b')\n",
    "s.add(2)\n",
    "print(s, type(s))\n",
    "\n",
    "s = {1, 2, 3, 4, 5}\n",
    "print(s, type(s))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{1, 2, 3, 4, 5} <class 'set'>\n",
      "{1, 2, 3, 4, 5} <class 'set'>\n"
     ]
    },
    {
     "ename": "KeyError",
     "evalue": "11",
     "output_type": "error",
     "traceback": [
      "\u001B[0;31m---------------------------------------------------------------------------\u001B[0m",
      "\u001B[0;31mKeyError\u001B[0m                                  Traceback (most recent call last)",
      "\u001B[0;32m/var/folders/mr/zthpdwtn37g2h4t97k6tv1pw0000gn/T/ipykernel_21377/2557696926.py\u001B[0m in \u001B[0;36m<module>\u001B[0;34m\u001B[0m\n\u001B[1;32m      8\u001B[0m \u001B[0;34m\u001B[0m\u001B[0m\n\u001B[1;32m      9\u001B[0m \u001B[0;31m# 在集合中删除指定的元素，删除某个不存在的元素是将报错\u001B[0m\u001B[0;34m\u001B[0m\u001B[0;34m\u001B[0m\u001B[0m\n\u001B[0;32m---> 10\u001B[0;31m \u001B[0ms\u001B[0m\u001B[0;34m.\u001B[0m\u001B[0mremove\u001B[0m\u001B[0;34m(\u001B[0m\u001B[0;36m11\u001B[0m\u001B[0;34m)\u001B[0m\u001B[0;34m\u001B[0m\u001B[0;34m\u001B[0m\u001B[0m\n\u001B[0m\u001B[1;32m     11\u001B[0m \u001B[0mprint\u001B[0m\u001B[0;34m(\u001B[0m\u001B[0ms\u001B[0m\u001B[0;34m,\u001B[0m \u001B[0mtype\u001B[0m\u001B[0;34m(\u001B[0m\u001B[0ms\u001B[0m\u001B[0;34m)\u001B[0m\u001B[0;34m)\u001B[0m\u001B[0;34m\u001B[0m\u001B[0;34m\u001B[0m\u001B[0m\n",
      "\u001B[0;31mKeyError\u001B[0m: 11"
     ]
    }
   ],
   "source": [
    "# 集合元素操作\n",
    "s = {1, 2, 3, 4, 5}\n",
    "print(s, type(s))\n",
    "\n",
    "# 在指定集合中丢弃某个元素，元素不存在时不会报错\n",
    "s.discard(11)\n",
    "print(s, type(s))\n",
    "\n",
    "# 在集合中删除指定的元素，删除某个不存在的元素是将报错\n",
    "s.remove(11)\n",
    "print(s, type(s))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{1, 2, 3, 4, 5} <class 'set'>\n",
      "{2, 3, 4, 5} <class 'set'>\n",
      "{2, 4, 5} <class 'set'>\n",
      "{2, 19, 4, 5, 7, 11} <class 'set'>\n",
      "set() <class 'set'>\n"
     ]
    }
   ],
   "source": [
    "# 集合元素的其他操作\n",
    "s = {1, 2, 3, 4, 5}\n",
    "print(s, type(s))\n",
    "\n",
    "# pop操作将删除某个集合元素，操作是无序的\n",
    "s.pop()\n",
    "print(s, type(s))\n",
    "\n",
    "# remove方法，在集合中删除指定的元素\n",
    "s.remove(3)\n",
    "print(s, type(s))\n",
    "\n",
    "# update方法，类似于union操作\n",
    "s.update({5, 7, 11, 19})\n",
    "print(s, type(s))\n",
    "\n",
    "# clear方法，清空集合元素\n",
    "s.clear()\n",
    "print(s, type(s))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "原始集合\n",
      "{1, 2, 3, 4, 5} <class 'set'>\n",
      "{3, 4, 5, 6, 7} <class 'set'>\n",
      "交集:\n",
      "{3, 4, 5}\n",
      "差集:\n",
      "{1, 2, 3, 4, 5} - {3, 4, 5, 6, 7} = {1, 2}\n",
      "{3, 4, 5, 6, 7} - {1, 2, 3, 4, 5} = {6, 7}\n",
      "并集:\n",
      "{1, 2, 3, 4, 5} union {3, 4, 5, 6, 7} = {1, 2, 3, 4, 5, 6, 7}\n",
      "是否子集:\n",
      "False\n",
      "是否父集:\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "# 集合运算\n",
    "s1 = {1, 2, 3, 4, 5}\n",
    "s2 = {3, 4, 5, 6, 7}\n",
    "\n",
    "print('原始集合')\n",
    "print(s1, type(s1))\n",
    "print(s2, type(s2))\n",
    "\n",
    "# 交集\n",
    "print('交集:')\n",
    "s = s1.intersection(s2)\n",
    "print(s)\n",
    "\n",
    "# 差集\n",
    "print(\"差集:\")\n",
    "s = s1.difference(s2)\n",
    "print(f\"{s1} - {s2} = {s}\")\n",
    "s = s2.difference(s1)\n",
    "print(f\"{s2} - {s1} = {s}\")\n",
    "\n",
    "# 并集\n",
    "print(\"并集:\")\n",
    "s = s1.union(s2)\n",
    "print(f\"{s1} union {s2} = {s}\")\n",
    "\n",
    "# 是否子集合\n",
    "print(\"是否子集:\")\n",
    "print(s1.issubset(s2))\n",
    "\n",
    "# 是否父集合\n",
    "print(\"是否父集:\")\n",
    "print(s1.issuperset(s2))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 元组\n",
    "\n",
    "略"
   ],
   "metadata": {
    "collapsed": false
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}